package engine.systems.ui.hudText
{
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.update.GameSystem;
	
	import flash.display.DisplayObjectContainer;
	import flash.text.TextField;
	
	/**
	 * The HUD Text System sets the child Text Field text of a Render display to match the current
	 * component property value that the HUD Text is monitoring.
	 */
	public class HUDTextSystem extends GameSystem
	{
		private var value:*;
		private var container:DisplayObjectContainer;
		private var text:TextField;
		
		public function HUDTextSystem()
		{
			super(HUDTextNode, updateNode);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		private function updateNode(node:HUDTextNode, time:Number):void
		{
			//Get the current value of component property the HUD Text is monitoring.
			value = node.hud.component[node.hud.property];
			
			//The Render display needs to be cast as a Display Object Container.
			container = node.render.display as DisplayObjectContainer;
			
			//Get the child Display Object with the given name.
			text = TextField(container.getChildByName(node.hud.childName));
			
			if(value is String) text.text = value;
			else
			{
				if(node.hud.round)	text.text = Math.round(value).toString();
				else				text.text = value.toFixed(node.hud.numDecimals).toString();
			}
		}
	}
}